package com.fu.shop.controller;

import com.fu.core.common.JsonResult;
import com.fu.core.pojo.Banner;
import com.fu.core.pojo.Notices;
import com.fu.core.pojo.Shop;
import com.fu.core.pojo.SystemNotices;
import com.fu.core.util.JsonUtil;
import com.fu.shop.service.ShopService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 付龙江
 * @date 2022/1/28 16:56
 */
@RestController
@RequestMapping("/shop")
@CrossOrigin
@Api(tags = "店铺管理")
public class ShopController {


    ShopService shopService;

    @Autowired
    public ShopController(ShopService shopService) {
        this.shopService = shopService;
    }

    @PostMapping(value = "/shopDetailMessage")
    @ApiOperation(value = "店铺基本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shopName", value = "店铺名称", dataType = "String", required = false),
            @ApiImplicitParam(name = "shopPhone", value = "客服手机号", dataType = "String", required = false),
            @ApiImplicitParam(name = "shopImg", value = "店铺详情介绍图片", dataType = "String", required = false),
            @ApiImplicitParam(name = "shopIntroduce", value = "店铺详情介绍", dataType = "String", required = false),
            @ApiImplicitParam(name = "shopLogo", value = "店铺logo", dataType = "String", required = false),
            @ApiImplicitParam(name = "shopAddress", value = "店铺地址", dataType = "String", required = false),
            @ApiImplicitParam(name = "shopId", value = "店铺编号", dataType = "Integer", required = false)
    })
    public Map<String,Object> shopDetailMessage(Shop shop) throws Exception {
        Shop newShop = null;
        if (shop == null || StringUtils.isEmpty(shop.getShopName())){
            newShop = shopService.selectShop();
        }else {
            newShop = shopService.updateShop(shop);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("shopMassage",newShop);
        return jsonResult.getMap();
    }

    @PostMapping(value = "/bannerMessage")
    @ApiOperation(value = "banner信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "map", value = "传空请传{}  传值请咨询我", dataType = "map", required = false)
    })
    public Map<String,Object> bannerMessage(@RequestBody Map<String,Object> map) throws Exception {

        List<Banner> banners = new ArrayList<>();
        if (map==null || map.size()<1){
           banners = null;
        }else {
            List mapBanners = (List)map.get("banners");
            if (mapBanners!=null){
                for (Object mapBanner : mapBanners) {
                    banners.add(JsonUtil.stringToObject(JsonUtil.objectToString(mapBanner),Banner.class));
                }
            }else {
                banners = null;
            }
        }
        List<Banner> nowBanners = shopService.bannerMessage(banners);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("banners",nowBanners);
        return jsonResult.getMap();
    }


    @PostMapping(value = "/releasingNotices")
    @ApiOperation(value = "系统通知中发布通知内容")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "notice", value = "通知内容", dataType = "String", required = true),
            @ApiImplicitParam(name = "title", value = "通知标题", dataType = "String", required = true)
    })
    public Map<String,Object> releasingNotices(String title,String notice) throws Exception {
        shopService.addSystemNotice(title,notice);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg("发布成功");
        jsonResult.setCode("200");
        return jsonResult.getMap();
    }

    @PostMapping(value = "/historyNotices")
    @ApiOperation(value = "历史通知")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "size", value = "每页条数", dataType = "Integer", required = true)
    })
    public Map<String,Object> historyNotices(Integer page, Integer size){
        PageInfo<SystemNotices> info = shopService.selectSystemNotices(page, size);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("total",info.getTotal());
        jsonResult.setField("Notices",info.getList());
        return jsonResult.getMap();
    }

    @PostMapping(value = "/deleteSystemNotices")
    @ApiOperation(value = "删除系统通知")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "systemId", value = "通知的id", dataType = "Integer", required = true)
    })
    public Map<String,Object> deleteHistoryNotices(Integer systemId) throws Exception {
        shopService.deleteSystemNotices(systemId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg("删除成功");
        jsonResult.setCode("200");
        return jsonResult.getMap();
    }

    @PostMapping(value = "/briefMessage")
    @ApiOperation(value = "消息处理界面")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "size", value = "每页条数", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "status", value = "消息状态单选：已回复 1  未回复 0", dataType = "Integer", required = false),
            @ApiImplicitParam(name = "name", value = "用户名或者电话模糊查询", dataType = "Integer", required = false)
    })
    public Map<String,Object> briefMessage(Integer status, String name, Integer page, Integer size){
        PageInfo<Map<String, Object>> mapPageInfo = shopService.selectUserNotices(status, name, page, size);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("total",mapPageInfo.getTotal());
        jsonResult.setField("message",mapPageInfo.getList());
        return jsonResult.getMap();
    }



    @PostMapping(value = "/historyMessages")
    @ApiOperation(value = "某个人的消息详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "size", value = "每页条数", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "Integer", required = true)
    })
    public Map<String,Object> historyMessages(Integer userId, Integer page, Integer size){
        PageInfo<Notices> noticesPageInfo = shopService.selectUserNoticesDetails(userId, page, size);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("total",noticesPageInfo.getTotal());
        jsonResult.setField("Notices",noticesPageInfo.getList());
        return jsonResult.getMap();
    }


    @PostMapping(value = "/releasingMessages")
    @ApiOperation(value = "回复消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "notices", value = "回复消息的内容", dataType = "String", required = true)
    })
    public Map<String,Object> releasingMessages(Integer userId, String notices) throws Exception {
        shopService.replayNotices(userId, notices);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("发送成功");
        return jsonResult.getMap();
    }

}
